Public Class formattach

    ' Tells if the application can be exited.
    Private m_bAllowClose As Boolean = False

    ' The vault is stored here.
    Private m_oVault As MFilesAPI.Vault = Nothing

    ' Scanned image.
    Private m_szScannedImagePath As String = ""

    ' Selected document.
    Private m_ovSelectedDocument As MFilesAPI.ObjectVersion = Nothing

    ' Tells if the back button is clicked.
    Private m_bBackButtonClicked As Boolean = False

    ' Initialize the form.
    Public Sub Initialize(ByVal oVault As MFilesAPI.Vault, ByVal szScannedImagePath As String)
        m_oVault = oVault
        m_szScannedImagePath = szScannedImagePath
    End Sub

    ' Tells if the back button is clicked.
    Function IsBackButtonClicked() As Boolean
        Return m_bBackButtonClicked
    End Function

    ' Fires when the form is loading.
    Private Sub formattach_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Empty the document name initially.
        labelDocument.Text = ""

        ' Disable add button.
        btnAdd.Enabled = False

        ' Set focus to find button.
        'btnFind.Focus()

    End Sub

    ' Fires when the for isclosing.
    Private Sub formattach_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        ' Prevent closing if not allowed.
        If Not m_bAllowClose Then
            e.Cancel = True
        End If

    End Sub

    ' Back button clicked.
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click

        ' Close the dialog.
        m_bBackButtonClicked = True
        m_bAllowClose = True
        Me.Close()

    End Sub

    ' Browse button clicked.
    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

        Dim o As New OpenFileDialog
        o.RestoreDirectory = True
        o.ShowDialog()

    End Sub

    ' Find button clicked.
    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

        Try

            ' Ask document ID.
            Dim dlg As New dlgdocumentid
            dlg.ShowDialog()
            Dim iID As Integer = dlg.GetDocumentID()

            ' Check that the ID is valid.
            If (iID <> -1) Then

                ' Try to find.
                Dim oObjVer As New MFilesAPI.ObjVer()
                oObjVer.SetIDs(0, iID, -1)
                Dim oObjectVersion As MFilesAPI.ObjectVersion = _
                    m_oVault.ObjectOperations.GetObjectInfo(oObjVer, True)

                ' Update label.
                labelDocument.Text = oObjectVersion.Title

                ' Store object version.
                m_ovSelectedDocument = oObjectVersion

                ' Enable add button.
                btnAdd.Enabled = True

            End If

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

    End Sub


    ' Add new file button clicked.
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        ' Checked out document is stored here.
        Dim ovCheckedOutDocument As MFilesAPI.ObjectVersion = Nothing

        Try

            ' Ask file name.
            Dim dlg As New dlgfilename
            dlg.ShowDialog()
            Dim szFileName As String = dlg.GetFileName

            ' Check that the file name is valid.
            If (szFileName <> "") Then

                ' Check the document out.
                ovCheckedOutDocument = m_oVault.ObjectOperations.CheckOut(m_ovSelectedDocument.ObjVer.ObjID)

                ' Ensure the document is a multifile.
                If (ovCheckedOutDocument.SingleFile = True) Then
                    m_oVault.ObjectOperations.SetSingleFileObject(ovCheckedOutDocument.ObjVer, False)
                End If

                ' Add new file.
                m_oVault.ObjectFileOperations.AddFile(ovCheckedOutDocument.ObjVer, szFileName, "pdf", m_szScannedImagePath)

                ' Check in the document.
                m_oVault.ObjectOperations.CheckIn(ovCheckedOutDocument.ObjVer)
                ovCheckedOutDocument = Nothing

                ' Close the dialog.
                m_bBackButtonClicked = False
                m_bAllowClose = True
                Me.Close()

            End If

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

        ' Do undo check out if needed.
        If (IsNothing(ovCheckedOutDocument) = False) Then
            m_oVault.ObjectOperations.UndoCheckout(ovCheckedOutDocument.ObjVer)
        End If

    End Sub


End Class